import modelExtend from 'dva-model-extend'
import {query, productquery, suitequery,save,sutquery,testhostquery,releasequery,executePlan,getResult} from '../services/execute'
import {pageModel} from './common'
import {notification,message} from 'antd'

export default modelExtend(pageModel, {

  namespace: 'execute',

  state: {
    productId: '',
    suiteName:[],
    planData:[],
    sutList:[],
    testHostList:[],
    releaseList:[],
    resultText:'暂无数据',
    targetKeylist:[]
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(location => {
        if (location.pathname === '/execute') {
          dispatch({
            type: 'query', payload: {
              status: 1,
              ...location.query,
            }
          })
        }
      })
    },
  },


  effects: {
    *query ({payload}, {call, put}) {

      if (payload.status !=3) {
      const products = yield call(productquery, payload)
      const suites = yield call(suitequery, payload)
      const data = yield call(query, payload)
      let isString = typeof data.data
      let isString2 = typeof products.data
      let isString3 = typeof suites.data
      if (data && products && suites) {

        if (isString == 'string' && isString2 == 'string' && isString3 == 'string') {
          let allList = []
          allList.data = JSON.parse(data.data)
          allList.productList = JSON.parse(products.data)
          allList.suiteList = JSON.parse(suites.data)
          yield put({
            type: 'querySuccess',
            payload: {
              list: allList,
              pagination: {
                current: Number(payload.page) || 1,
                pageSize: 10,
                total: data.total,
                showSizeChanger: false,
                showQuickJumper: false
              },
            },

          })
        }
      } else {
        throw data
      }
    }else{
        const sutList = yield call(sutquery, payload)
        const testHostList = yield call(testhostquery, payload)
        const releaseList = yield call(releasequery, JSON.parse(sessionStorage.getItem('currentPlan')).productId)
        let isString = typeof sutList.data
        let isString2 = typeof testHostList.data
        let isString3 = typeof releaseList.data
        if (sutList && testHostList && releaseList) {
          if(isString == 'string' && isString2 == 'string'){
            let params=[]
            params.currentPlan=[]
            params.currentPlan.push(JSON.parse(sessionStorage.getItem('currentPlan')))
            params.sutList=JSON.parse(sutList.data)
            params.testHostList=JSON.parse(testHostList.data)
            params.releaseList=JSON.parse(releaseList.data)

            yield put({ type: 'changePlanData',payload:params })
          }
        }
      }


    },
    *save ({ payload }, { call, put }) {
      const data = yield call(save, payload)
      if (data.success && data.code === 0) {
        // yield put({ type: 'hideModal' })
        // yield put({ type: 'query' })
        message.success('保存成功！');
      } else {
        throw data.msg
      }
    },
    *executePlan ({ payload }, { call, put }) {
      const data = yield call(executePlan, payload)
      if (data.success && data.code === 0) {
        notification.open({
          message: 'Notification Title',
          description: '任务已派发，请到结果页面查询执行结果',
        });
        // yield put({ type: 'executeResult',payload:data.testResultId })
      } else {
        throw data.msg
      }
    },
    *executeResult ({ payload }, { call, put }) {
      const data = yield call(getResult, payload)
      if (data.success && data.code === 0) {
        yield put({ type: 'changeResult',payload:{resultText:data.result} })
      } else {
        yield put({ type: 'executeResult',payload })
        // throw data.msg
      }
    },
  },
  reducers: {

    changeSuite(state, { payload }){
      return { ...state, ...payload, productId: payload.productId,level:payload.level,targetKeylist:payload.targetKeylist}
    },
    changePlanData(state, { payload }){
      return { ...state, ...payload, planData: payload.currentPlan,sutList:payload.sutList,testHostList:payload.testHostList,releaseList:payload.releaseList}
    },
    changeResult(state, { payload }){
      return { ...state, ...payload, resultText: payload.resultText}
    },
    clearResult(state, { payload }){
      return { ...state, ...payload, resultText: '暂无数据'}
    },
    changeTarget(state, { payload }){
      return { ...state, ...payload, targetKeylist: payload.targetKeylist}
    }
  },
})
